package com.luck.easyfoodbatch.callback;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.entity.User;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.MD5Utils;
import com.lzy.okhttputils.callback.AbsCallback;
import com.lzy.okhttputils.request.BaseRequest;

/**
 * ================================================
 * 作    者：罗孟伟
 * 版    本：1.0
 * 创建日期：2016/4/8
 * 描    述：
 * 修订历史：该类主要用于在所有请求之前添加公共的请求头或请求参数，例如登录授权的 token,使用的设备信息等
 * ================================================
 */
public abstract class CommonCallback<T> extends AbsCallback<T> {
    @Override
    public void onBefore(BaseRequest request) {
        super.onBefore(request);
//        //如果账户已经登录，就添加 token 等等
        User user = (User) App.getInstance().readObject(HttpUtil.USER_KEY);
        String auth_token = "";
        String uid = "";
        if (user != null) {
            auth_token = user.getAuthToken();
            uid = user.getUid() + "";
        } else {
            auth_token = "";
            uid = "";
        }
        String time = String.valueOf(System.currentTimeMillis()).substring(0, 10);
        request.params(HttpUtil.TIME, time)
                .params(HttpUtil.TOKEN, MD5Utils.Md5(time + HttpUtil.KEY))
                .params(HttpUtil.AUTHTOKEN, auth_token);

    }
}
